package DatabaseManaging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import DatabaseManaging.Models.LangChan;
import DatabaseManaging.Models.LanguageBuilder;

public class JdbcLanguageDao implements ParametersDAO{
	private JdbcTemplate jdbcTemplate;

	public JdbcLanguageDao() {
	}
	
	@Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
	
	@Override
	public ArrayList<Object> fromMapToListElements(
			ArrayList<Map<String, Object>> resultQuery) {
		ArrayList<Object> resultList = new ArrayList<Object>();
        for(int i=0;i<resultQuery.size();i++){
    	  LangChan language=LanguageBuilder.build(resultQuery.get(i));
    	  System.out.println("Got "+language);
    	  resultList.add(language);
        }
		return resultList;
	}

	@Override
	public ArrayList<Object> findElementby(String name) {
		System.out.println("Find language by " + name);
		String sql = "SELECT * from akn_language_dim where language_name=?";
		Object[] obj={name};
		ArrayList<Map<String, Object>> resultQuery = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql, obj);
		ArrayList<Object> resultList = fromMapToListElements(resultQuery);

		return resultList;
	}

	@Override
	public void insertElement(Object inputlanguage) {
    	LangChan lang=new LangChan();
    	lang=(LangChan) inputlanguage;
    	String sql="insert into akn_language_dim " +
    	        "(language_name,language_abbr1,language_abbr2,language_abbr3," +
    	        "language_abbr4,language_abbr5) values(?,?,?,?,?,?) ";   	
    	Object[] obj = genObjectToInsert(lang);
    	this.jdbcTemplate.update(sql,obj);		
	}
	private Object[] genObjectToInsert(LangChan prod) {
		Object[] obj=new Object[6];
    	obj[0]=prod.getName();
		int j;
		for(j=0;j<prod.getAbbs().size();j++){
		obj[j+1]=prod.getAbbs().get(j);
		}
		while(j<5){
			obj[j+1]="";
			j++;
		}
		return obj;
	}

	@Override
	public void updateAbbsByName(String id, ArrayList<String> langAbbs) {
    	for(int i=0;i<langAbbs.size();i++){
    		this.jdbcTemplate.update("UPDATE akn_language_dim SET language_abbr?=? WHERE language_dim_id=?",new Object[] {i+1,langAbbs.get(i),id});
    	}
	}

	@Override
	public Map<ArrayList<String>, String> getMapwithAllElements() {
		String sql = "SELECT * from akn_language_dim";
		ArrayList<Map<String, Object>> resultQuery = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
		HashMap<ArrayList<String>, String>  resultList = toMapElements(resultQuery);
		return resultList;
	}
	private HashMap<ArrayList<String>, String> toMapElements(
			ArrayList<Map<String, Object>> resultQuery) {
		HashMap<ArrayList<String>,String> resultMap=new HashMap<ArrayList<String>,String>();
		for(int i=0;i<resultQuery.size();i++){
        	Map<String, Object> mapElements=resultQuery.get(i);
        	Object id=mapElements.get("language_dim_id");
        	ArrayList<String> paramLanguages=new ArrayList<String>();
        	paramLanguages.add((String)mapElements.get("language_name"));
        	for(int j=1;j<6;j++){
        		String abb=(String)mapElements.get("language_abbr"+j);
        		if(!abb.isEmpty()){
        			paramLanguages.add(abb);
        		}
        	}
        	resultMap.put(paramLanguages,id.toString());
        }
		return resultMap;
	}
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("application-context-persist.xml");
		JdbcLanguageDao dao=(JdbcLanguageDao) context.getBean("JdbcLanguagesDao");
//		System.out.println(dao.findElementby("russian"));//PASSED
//		LangChan lang=new LangChan();
//		lang.setName("Castellano");
//		ArrayList<String> abbs=new ArrayList<String>();
//		abbs.add("cast");
//		abbs.add("castellano");
//		lang.setAbbs(abbs);
//		dao.updateAbbsByName("21",abbs);
		System.out.println(dao.getMapwithAllElements());
		
	}

}
